using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using UnityEngine;

public class PlayerInShopManager : MonoBehaviour
{
	public static PlayerInShopManager instance;

	public Transform weaponHolder;

	public Transform weaponHolderLeft;

	public GameObject jetPack;

	public GameObject jetPackParticle;

	public string curSkin;

	[Range(0f, 1f)]
	public float invisibleAlpha = 0.3f;

	[SerializeField]
	private PlayerMecanimWrapper mecanimWrapper;

	private WeaponMecanimWrapper weaponMecanimWrapper;

	private GameObject _instantiatedWeapon;

	private BaseWeapon currentWeaponScript;

	private IEnumerator _playAnim;

	private Tweener levetationUp;

	private Tweener levetationDown;

	private bool inAir;

	private bool hasWeapon;

	private bool initialized;

	private void Awake()
	{
		instance = this;
		Init();
	}

	private void Init()
	{
		if (!initialized)
		{
			initialized = true;
			mecanimWrapper = GetComponentInChildren<PlayerMecanimWrapper>();
			mecanimWrapper.shopPlayer = true;
			Reset();
		}
	}

	public void InitAnim()
	{
		if (_playAnim == null && base.gameObject.activeInHierarchy)
		{
			_playAnim = PlayAnimation();
			StartCoroutine(_playAnim);
		}
	}

	private void OnDisable()
	{
		if (_playAnim != null)
		{
			StopCoroutine(_playAnim);
			_playAnim = null;
		}
	}

	public void DeinitAnim()
	{
	}

	private void Reset()
	{
		jetPack.SetActive(false);
		jetPackParticle.SetActive(false);
		mecanimWrapper.SwitchUpperLayer(true);
		mecanimWrapper.SetIsGrounded(true);
	}

	public void InstantiateWeapon(string key)
	{
	}

	public void Invisible(bool activate)
	{
		List<Material> list = new List<Material>();
		bool flag = false;
		Material material;
		Material material2;
		if (activate)
		{
			material = new Material(Shader.Find("Transparent/Diffuse"));
			material2 = new Material(Shader.Find("Transparent/Diffuse"));
			material.color = new Color(0f, 0f, 0f, invisibleAlpha);
			material2.color = new Color(0f, 0f, 0f, invisibleAlpha);
		}
		else
		{
			material = new Material(Shader.Find("Mobile/Diffuse"));
			material2 = new Material(Shader.Find("Diffuse"));
			material.color = Color.white;
			material2.color = Color.white;
		}
		list.Add(material);
		Renderer[] componentsInChildren = base.gameObject.GetComponentsInChildren<Renderer>(true);
		Renderer[] array = componentsInChildren;
		foreach (Renderer renderer in array)
		{
			if (renderer.gameObject.tag == "Weapon")
			{
				if (!(renderer.gameObject.GetComponent<ParticleSystem>() == null) || renderer.gameObject.name.Equals("shadow"))
				{
					continue;
				}
				flag = false;
				foreach (Material item in list)
				{
					if (item.mainTexture == renderer.material.mainTexture)
					{
						flag = true;
						renderer.material = item;
						break;
					}
				}
				if (!flag)
				{
					Material material3 = new Material(material);
					material3.mainTexture = renderer.material.mainTexture;
					list.Add(material3);
					renderer.material = material3;
				}
			}
			else
			{
				if (material2.mainTexture == null)
				{
					material2.mainTexture = settings.getTekSkin();
				}
				renderer.material = material2;
			}
		}
	}

	public void ShowWeapon(string key)
	{
		Init();
		if (_instantiatedWeapon != null)
		{
			Object.Destroy(_instantiatedWeapon);
		}
		hasWeapon = true;
		GameObject gameObject = Resources.Load("Weapons/" + key) as GameObject;
		if (gameObject == null)
		{
			gameObject = Resources.Load("Weapons/knuckles") as GameObject;
			hasWeapon = false;
		}
		_instantiatedWeapon = (GameObject)Object.Instantiate(gameObject, weaponHolder.position, weaponHolder.rotation);
		currentWeaponScript = _instantiatedWeapon.GetComponent<BaseWeapon>();
		currentWeaponScript.isMobWeapon = true;
		currentWeaponScript.chargerAmmo = currentWeaponScript.maxChargerAmmo;
		currentWeaponScript.Init(1, weaponHolder, weaponHolderLeft, GetComponentInChildren<Collider>());
		weaponMecanimWrapper = currentWeaponScript.weaponMecanimWrapper;
		mecanimWrapper.SetWeapon(weaponMecanimWrapper.overrideController, currentWeaponScript.reloadTime, currentWeaponScript.timeBetweenShots, weaponMecanimWrapper.pullable);
		weaponMecanimWrapper.Init(mecanimWrapper, currentWeaponScript.reloadTime, currentWeaponScript.timeBetweenShots, true);
		if (_instantiatedWeapon.GetComponentInChildren<ParticleSystem>() != null)
		{
			PartickleMagick partickleMagick = _instantiatedWeapon.AddComponent<PartickleMagick>();
			ParticleSystem[] array = (partickleMagick.listParticle = _instantiatedWeapon.GetComponentsInChildren<ParticleSystem>());
			for (int i = 0; i < array.Length; i++)
			{
				if (key.Equals("Freezer"))
				{
					array[i].gameObject.SetActive(false);
					continue;
				}
				array[i].startSize *= 0.4f;
				array[i].startSpeed *= 0.35f;
			}
			partickleMagick.Init();
		}
		Component[] componentsInChildren = weaponHolder.GetComponentsInChildren<Transform>();
		Component[] componentsInChildren2 = weaponHolderLeft.GetComponentsInChildren<Transform>();
		for (int j = 0; j < componentsInChildren.Length; j++)
		{
			componentsInChildren[j].gameObject.layer = 26;
		}
		for (int k = 0; k < componentsInChildren2.Length; k++)
		{
			componentsInChildren2[k].gameObject.layer = 26;
		}
		InitAnim();
	}

	public BaseWeapon ReturnWeaponByid(string keyForShow)
	{
		return Resources.Load<BaseWeapon>("Weapons/" + keyForShow);
	}

	public void ChangeSkin(string id = "", GameObject objForSkin = null)
	{
		Init();
		Renderer[] array = ((!(objForSkin != null)) ? GetComponentsInChildren<Renderer>() : objForSkin.GetComponentsInChildren<Renderer>());
		if (id.Equals(string.Empty))
		{
			id = MarketController.Instance.GetEquipedSkinID();
		}
		if (array.Length != 0)
		{
			Texture skinTexture = MarketController.Instance.GetSkinTexture(id);
			Renderer[] array2 = array;
			foreach (Renderer renderer in array2)
			{
				if (renderer.gameObject.tag != "Weapon")
				{
					renderer.material.mainTexture = skinTexture;
				}
			}
		}
		InitAnim();
	}

	private IEnumerator PlayAnimation()
	{
		yield return StartCoroutine(BCWUtils.WaitForRealSeconds(1f));
		while (true)
		{
			if (hasWeapon)
			{
				weaponMecanimWrapper.PlayDemoAnim();
				mecanimWrapper.PlayDemoAnim();
			}
			yield return StartCoroutine(BCWUtils.WaitForRealSeconds(10f));
		}
	}
}
